<template>
    <div>
        <!--查询条件start --111s-->
        <el-form :inline="true"
                 :model="query"
                 class="query-form"
                 size="small">
            <el-form-item class="query-form-item">
                <el-input v-model="query.searchtext"
                          placeholder="商户账号"
                          prefix-icon="el-icon-search"
                          @keyup.enter.native="onSubmit"
                          clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary"
                           icon="el-icon-refresh"
                           @click="onRest"></el-button>
                <el-button type="primary"
                           icon="el-icon-search"
                           @click="onSubmit">查询</el-button>
            </el-form-item>
        </el-form>
        <!--查询条件end-->

        <!--列表start --->
        <el-table ref="multipleTable"
                  v-loading="loading"
                  :data="list"
                  :header-cell-style="{background:'#F4F4F5',color:'#606266'}"
                  @selection-change="handleSelectionChange"
                  border>
            <el-table-column label="序号" prop="commodityname" width="150" align="center">
                <template slot-scope="scope">
                    <span>{{scope.$index + 1}}</span>
                </template>
            </el-table-column>
            <el-table-column label="商户账号" prop="usermobile" align="center"/>
            <!-- <el-table-column label="轮播图"
                                prop="commodityIcon"
                                align="center">
                <template slot-scope="scope">
                    <el-popover v-if="scope.row.commodityIcon"
                                placement="right"
                                trigger="hover">
                        <img :src="scope.row.commodityIcon"
                                style="width:400px;border-radius:3px;">
                        <img slot="reference"
                                :src="scope.row.commodityIcon"
                                style="max-height: 80px;max-width: 50px;border-radius:5px;">
                    </el-popover>
                    <span v-else>-</span>
                </template>
            </el-table-column> -->
            <el-table-column label="目前等级"
                             :show-overflow-tooltip="true"
                             align="center">
                <template slot-scope="scope">
                    <span class="darkTag">{{ scope.row | levelFilter }}</span>
                </template>
            </el-table-column>
            <el-table-column label="当前业绩"
                             :show-overflow-tooltip="true"
                             prop="performancecount"
                             align="center">
            </el-table-column>
            <el-table-column label="申请成为"
                             width="100"
                             :show-overflow-tooltip="true"
                             align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.status == 0"  class="darkTag">{{ scope.row | promotionFilter }}</span>
                    <span v-else> - </span>
                </template>
            </el-table-column>
            <el-table-column label="审核状态"
                             width="100"
                             prop="status"
                             :show-overflow-tooltip="true"
                             align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.status == 0" style="margin-left:20px;color:#ffa659;">待审核</span>
                    <span v-else-if="scope.row.status == 1" style="margin-left:20px;color:#4acf1e;">已通过</span>
                    <span v-else-if="scope.row.status == 2" style="margin-left:20px;color:#dc0101;">已驳回</span>
                </template>
            </el-table-column>
            <el-table-column label="操作"
                             align="center">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.status == 0" type="primary"
                               size="mini"
                               @click.native="upgradeExamine(scope.row,1)">通过</el-button>
                    <el-button  v-if="scope.row.status == 0" type="danger"
                                size="mini"
                                @click.native="upgradeExamine(scope.row,2)">拒绝</el-button>
                    <span v-if="scope.row.status != 0"> - </span>
                </template>
            </el-table-column>
        </el-table>


        <!--分页模块start-->
        <el-pagination class="pagination-container"
                       background
                       :page-size="query.pageSize"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       layout="total,sizes,prev, pager, next, jumper"
                       :current-page="query.currentPage"
                       :page-sizes="[10, 20, 30, 50]"
                       :total="totalNumber"></el-pagination>
        <!--分页模块end-->
    </div>
</template>
<script>
    import {
        getPartnerApplicationList, // 商户品牌合伙公司合伙人申请列表
        merchantPartnerApplicationToExamine // 商户品牌合伙公司合伙人申请审核
    } from "../../api/levelAudit/levelAudit.js";
    import {
        deleteDailyNew,
        batchAddDailyNew
    } from "../../api/activityCommodity/activityCommodity.js";
    import {BASE_URL} from "../../config/app";
    /*用作表单的数据初始化*/
    const formJson = {
        ids: ""
    };

    export default {
        components: {},
        data() {
            return {
                uploadUrl: BASE_URL + '/goldendays-currency/tool/uploadpro', //上传图片
                uploadData: [],
                query: {
                    currentPage: 1,
                    searchtext: null,
                    pageSize: 10
                },
                checkQuery: {
                    currentPage: 1,
                    pageSize: 10
                },
                commodityIdArr: [],
                checkTotalNumber: 0, //分页总页数
                formName: null,
                formMap: {
                    add: "添加",
                    edit: "编辑"
                },
                merchantslist: [],
                merchantsgoodslist: [],
                list: [], //列表集合
                totalNumber: 0, //分页总页数
                loading: true, //列表加载
                checkLoading: false,
                formSumbitLoding: false, //表单提交加载
                cancelToggle: false, //列表是否全选
                multipleSelection: [], //多选集合
                dialogAddOrEdit: false, //新增或编辑弹框开关
                dialogAddOrEditName: "", //新增或编辑弹框名字
                formData: formJson, //新增或修改的对象
                rules: {
                    //表单校验
                    // userId: {
                    //     required: true,
                    //     message: "请选择商户",
                    //     trigger: "blur"
                    // },

                    ids: {
                        required: true,
                        message: "请选择商品",
                        trigger: "blur"
                    }
                }
            };
        },

        methods: {
            //上传图片
            handleSuccess(response, file) {
                this.isSelect = false;
                console.log("上传成功");
                if (response.status === 1) {
                    const filePath = response.data;
                    this.formData.topcategorypic = filePath;
                    console.info(filePath + file);
                    //  alert(JSON.stringify(this.uploadData));
                    return;
                }

                this.$message.error("上传文件报错");
            },
            handleChange(file) {
                this.fileName = file.name ? file.name : "";
                return false;
            },

            /*获取列表事件*/
            getPartnerApplicationList() {
                this.loading = true;
                //   getTopList
                getPartnerApplicationList(this.query)
                    .then(response => {
                        if (response.code != 0) {
                            this.$message.error(response.message);
                            this.loading = false;
                            return false;
                        }
                        console.log(response);
                        this.list = response.data.list || [];
                        this.totalNumber = response.data.totalNumber || 0;
                        setTimeout(() => {
                            this.loading = false;
                        }, 0.3 * 1000);
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                    });
            },

            /*新增或者编辑提交事件*/
            formSumbit() {
                //表单校验通过
                let commodityIds = this.commodityIdArr.toString();
                this.formData.commodityIds = commodityIds;
                console.info(this.formData);
                this.$refs["ruleForm"].validate(valid => {
                    // console.log(valid)
                    if (valid) {
                        this.formSumbitLoding = true;
                        this.formData.inputdata = JSON.stringify(this.formData);
                        batchAddDailyNew(this.formData)
                            .then(response => {
                                this.formSumbitLoding = false;
                                if (response.code != 0) {
                                    this.$message.error(response.message);
                                    return false;
                                }
                                this.merchantsgoodslist = [];
                                this.checkTotalNumber = 0;
                                this.$message.success("操作成功");
                                this.hideDialogAddOrEdit();
                                // 加载表格数据
                                this.getPartnerApplicationList();
                            })
                            .catch(() => {
                                this.formSumbitLoding = false;
                            });
                    }
                });
                //new URLSearchParams(this.query)  提交数据格式不正确时可以转一下
            },

            closeCheck() {
                this.dialogAddOrEdit = false;
                this.merchantsgoodslist = [];
                this.checkTotalNumber = 0;
            },
            /*列表查询条件事件*/
            onSubmit() {
                this.query.currentPage = 1;
                this.getPartnerApplicationList();
            },

            /*刷新*/
            onRest() {
                //刷新事件需要重置查询条件
                this.query = {
                    currentPage: 1,
                    pageSize: 10
                };
                this.getPartnerApplicationList();
            },

            //显示 查看弹框
            detailForm(index, row) {
                if (row !== null) {
                    console.info(row);
                    this.dialogAddOrEdit = true;
                    this.formData = Object.assign({}, row);
                    let data = {
                        name: "",
                        url: ""
                    };
                    if (row.topcategorypic) {
                        this.uploadData = [];
                        let nameurl = row.topcategorypic;
                        data.url = nameurl;
                        data.name = nameurl.substring(nameurl.lastIndexOf("/") + 1);
                        this.uploadData.push(data);
                    }

                    this.dialogAddOrEditName = "编辑";
                }
            },

            /*显示 -新增或者编辑弹框*/
            showDialogAddOrEdit(index, row) {
                console.log(index, row);
                this.formData = Object.assign({}, formJson);
                this.dialogAddOrEdit = true;
                this.dialogAddOrEditName = "编辑";
            },

            /*隐藏 -新增或编辑弹框*/
            hideDialogAddOrEdit() {
                this.dialogAddOrEdit = false;
                this.merchantsgoodslist = [];
                //隐藏弹框时需要清除弹框内容
                if (this.$refs["ruleForm"]) {
                    this.$refs["ruleForm"].resetFields(); //清除formData表单
                    this.$refs["ruleForm"].clearValidate(); //清除表单验证
                }
                //如果上传文件，还需要清除文件列表
            },
            // 弹窗下拉选项变更获取商品
            getMerchantsGoods() {
                this.merchantsgoodslist = [];
                this.formData.ids = "";
                //   let query = {
                //     currentPage: 1,
                //     pageSize: 1000,
                //     userId: this.formData.userId,
                //   }
                this.checkQuery.userId = this.formData.userId;
                console.info(this.checkQuery);
                this.checkLoading = true;
                getMerchantsGoods(this.checkQuery)
                    .then(response => {
                        console.log(response);
                        if (response.code != 0) {
                            this.$message.error(response.message);
                            this.checkLoading = false;
                            return false;
                        }
                        this.checkLoading = false;
                        this.checkTotalNumber = response.data.totalNumber || 0;
                        this.merchantsgoodslist = response.data.list || [];
                        console.log(this.merchantsgoodslist);
                    })
                    .catch(() => {
                        this.checkLoading = false;
                        this.merchantsgoodslist = [];
                    });
            },

            /*点击页数*/
            handleCurrentChange(val) {
                this.query.currentPage = val;
                this.getPartnerApplicationList();
            },

            /*选择每页条数【10,20,30,40,50】*/
            handleSizeChange(val) {
                this.query.pageSize = val;
                this.getPartnerApplicationList();
            },
            /*弹窗 选择每页条数【10,20,30,40,50】*/
            checkHandleSizeChange(val) {
                this.checkQuery.pageSize = val;
                this.getMerchantsGoods(); // 重新查询列表
            },
            /*弹窗 点击页数*/
            checkHandleCurrentChange(val) {
                this.checkQuery.currentPage = val;
                this.getMerchantsGoods(); // 重新查询列表
            },
            /*弹窗 列表复选框选择事件*/
            checkHandleSelectionChange(val) {
                console.log(val);
                let arr = [];
                val.forEach((value, index) => {
                    console.log(index);
                    arr.push(value.commodityid);
                });
                this.commodityIdArr = arr;
            },
            /*取消全选事件*/
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },

            /*列表复选框选择事件*/
            handleSelectionChange(val) {
                this.multipleSelection = val;
                if (val == "") {
                    this.cancelToggle = false;
                    return;
                }
                this.cancelToggle = true;
            },

            /*全选删除事件*/
            delGroup() {
                console.log(this.multipleSelection);
                var ids = this.multipleSelection
                    .map(item => item.dailynewid)
                    .join(); //获取所有选中行的id组成的字符串，以逗号分隔
                if (ids == "") {
                    this.$message({
                        message: "请先选择",
                        type: "warning"
                    });
                    return;
                }
                this.handleDel(ids);
            },

            /*单击删除事件*/
            handleDel(ids) {
                if (ids) {
                    this.$confirm("确认要删除吗?", "提示", {
                        type: "warning"
                    })
                        .then(() => {
                            this.loading = true;
                            var params = new URLSearchParams();
                            params.append("dailyNewIds", ids);
                            deleteDailyNew(params)
                                .then(response => {
                                    this.loading = false;
                                    if (response.code) {
                                        this.$message.error(response.message);
                                        return false;
                                    }
                                    this.$message.success("删除成功");
                                    // 刷新数据
                                    //this.list.splice(index, 1);
                                    this.getPartnerApplicationList();
                                })
                                .catch(() => {
                                    this.loading = false;
                                });
                        })
                        .catch(() => {
                            this.$message.warning("已取消");
                        });
                }
            },
            // 审核
            upgradeExamine(row, status) {
                console.log(row);
                let data = {
                    relationid: row.relationid, // id
                    status: status, // 审核状态
                    memberlevel: null, // 小段位等级  0 普通 1 高级 2首席
                    performancelevel: null // 大段位等级 1 品牌 2 省级 3 市级 4 县级 5 商城
                };
                if (status == 1) {
                    // if(row.memberlevel == 0 && row.performancelevel != 5){
                    //     data.memberlevel = 1;
                    //     data.performancelevel = row.performancelevel;
                    // }else{
                    //     data.memberlevel = 0;
                    //     data.performancelevel = row.performancelevel - 1;
                    // }
                    if (row.performancelevel != 5) {
                        if (row.memberlevel == 0) {
                            data.memberlevel = 1;
                        } else if (row.memberlevel == 1) {
                            data.memberlevel = 2;
                        } else {
                            this.$message.error("已经是首席！！！");
                            return false;
                        }
                        data.performancelevel = row.performancelevel;
                    } else {
                        data.performancelevel = 4;
                        data.memberlevel = 0;
                    }
                } else {
                    data.memberlevel = row.memberlevel;
                    data.performancelevel = row.performancelevel;
                }
                console.log(data);
                this.loading = true;
                merchantPartnerApplicationToExamine(data)
                    .then(response => {
                        this.loading = false;
                        if (response.code) {
                            this.$message.error(response.message);
                            return false;
                        }
                        this.$message.success("审核成功！");
                        this.getPartnerApplicationList();
                        this.loading = false;
                    })
                    .catch(err => {
                        this.loading = false;
                    });
            }
        },
        filters: {
            levelFilter(row) {
                let levelName = "";
                console.log(row.performancelevel);
                switch (row.performancelevel) {
                    case 0:
                        row.memberlevel === 0
                            ? (levelName = "平台普通")
                            : (levelName = "平台高级");
                        break;
                    case 1:
                        row.memberlevel === 0
                            ? (levelName = "品牌普通")
                            : (levelName = "品牌高级");
                        break;
                    case 2:
                        row.memberlevel === 0
                            ? (levelName = "省普通")
                            : (levelName = "省高级");
                        break;
                    case 3:
                        row.memberlevel === 0
                            ? (levelName = "市普通")
                            : (levelName = "市高级");
                        break;
                    case 4:
                        row.memberlevel === 0
                            ? (levelName = "县普通")
                            : (levelName = "县高级");
                        break;
                    case 5:
                        levelName = "商城";
                        break;
                }
                return levelName;
            },
            promotionFilter(row) {
                let levelName = "";
                switch (row.performancelevel) {
                    case 0:
                        levelName = "平台高级";
                        break;
                    case 1:
                        row.memberlevel === 0
                            ? (levelName = "品牌高级")
                            : (levelName = "平台普通");
                        break;
                    case 2:
                        row.memberlevel === 0
                            ? (levelName = "省高级")
                            : (levelName = "品牌普通");
                        break;
                    case 3:
                        row.memberlevel === 0
                            ? (levelName = "市高级")
                            : (levelName = "省普通");
                        break;
                    case 4:
                        row.memberlevel === 0
                            ? (levelName = "县高级")
                            : (levelName = "市普通");
                        break;
                    case 5:
                        levelName = "县普通";
                        break;
                }
                return levelName;
            }
        },
        created() {
            this.getPartnerApplicationList();
            //
        }
    };
</script>
<style lang="scss"  scoped>
    .darkTag {
        color: #304156;
        // text-decoration: underline;
        // display: inline-block;
        // padding: 1px 5px;
        // border: 1px solid #304156;
        // border-radius: 3px;
        text-shadow: 0 2px 3px rgba(48, 65, 86, 0.3);
        cursor: pointer;
    }
    span.darkTag + span {
        margin-left: 10px;
    }
</style>
